﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using Oracle.DataAccess.Client;
using System.Data;
using System.Configuration;
using System.Data.OracleClient;

namespace trangbaocao.Models
{
    public class General
    {
        public static OracleConnection ConnectDataBase()
        {
            OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["trangbaocao"].ConnectionString);
            return conn;
        }
        public static DataSet Lib_GetDataSet(string sql)
        {
            OracleConnection conn = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn);
            OracleDataAdapter oda = new OracleDataAdapter(omd);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            oda.Fill(ds, "0");
            conn.Close();
            return ds;
        }
        public static void Lib_ExecuteNonQuery(string sql)
        {
            OracleConnection conn = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn);
            try
            {
                conn.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            omd.ExecuteNonQuery();
            conn.Close();

        }
        public static OracleDataReader Lib_GetDataReader(string sql)
        {
            OracleConnection conn = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn);
            try
            {
                conn.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            OracleDataReader odr = omd.ExecuteReader();
            return odr;
        }
        public static int Lib_CheckExist(string sql)
        {
            OracleConnection conn = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn);
            try
            {
                conn.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            OracleDataReader odr = omd.ExecuteReader();            
            DataTable dt = new DataTable();
            dt.Load(odr);
            return dt.Rows.Count;

        }
    }
}